package org.xmlrpc.android;

import info.marlan.sipgate.WidgetProvider;

import org.apache.http.conn.HttpHostConnectException;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;

public class XMLRPCMethod extends Thread {

	public final static String ERROR_MESSAGE = "error.message";
	public final static String ERROR_NR = "error.nr";
	public final static int STATUSCODE_XMLRPCEXCEPTION = 1011;
	public final static int STATUSCODE_MISSING_STATUSCODE = 1111;
	public final static int STATUSCODE_SUCCESS = 200;
	public final static int STATUSCODE_NO_CONNECTION = 1010;
	public final static int STATUSCODE_UNAUTHORIZED = 401;
	private String method;
	private Object[] params;
	private Handler handler;
	private XMLRPCMethodCallback callBack;
	private XMLRPCClient client;
	public XMLRPCMethod(String method, XMLRPCMethodCallback callBack, XMLRPCClient client, Handler handler) {
		this.method = method;
		this.callBack = callBack;
		this.handler = handler;
		this.client = client;
	}
	
	public static boolean isBackgroundDataEnabled(Context context){
		ConnectivityManager connectivity = (ConnectivityManager) context
		.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectivity.getActiveNetworkInfo();
		return info == null || connectivity.getBackgroundDataSetting();
	}
	
	public static boolean isConnected(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
//		TelephonyManager mTelephony = (TelephonyManager) context
//				.getSystemService(Context.TELEPHONY_SERVICE);

		
		NetworkInfo info = connectivity.getActiveNetworkInfo();
		if (info == null || !connectivity.getBackgroundDataSetting() ) {
			//Log.v(WidgetProvider.LOGTAG,"nobackgrounddata");
			return false;
		}
		
		if(info.isConnectedOrConnecting()){
			return true;
		}
		
		return false;
//		int netType = info.getType();
//	//	int netSubtype = info.getSubtype();
//		if (netType == ConnectivityManager.TYPE_WIFI && info.isConnectedOrConnecting()) {
//			return HotelClient.NETWORK_AVAILABLE;
//		} else if (netType == ConnectivityManager.TYPE_MOBILE && info.isConnectedOrConnecting()){
//		//	if( !mTelephony.isNetworkRoaming()){
//				return HotelClient.NETWORK_AVAILABLE;
////			}else{
////				return HotelClient.NETWORK_ROAMING;
////				
////			}
//		}
			
						
	}
	
	public void call() {
		call(null);
	}
	public void call(Object[] params) {
//		status.setTextColor(0xff80ff80);
//		status.setError(null);
//		status.setText("Calling host " + uri.getHost());
//		tests.setEnabled(false);
		this.params = params;
		start();
	}
	
	public void sendMessage(String msg, int nr){
		Message message = new Message();
		Bundle bundle = new Bundle();
		bundle.putString(XMLRPCMethod.ERROR_MESSAGE, msg);
		bundle.putInt(XMLRPCMethod.ERROR_NR, nr);
		message.setData(bundle);
		handler.sendMessage(message);
	}
	
	
	@Override
	public void run() {
		try {
			final long t0 = System.currentTimeMillis();
			final Object result = client.callEx(method, params);
			final long t1 = System.currentTimeMillis();
			handler.post(new Runnable() {
				public void run() {
//					tests.setEnabled(true);
//					status.setText("XML-RPC call took " + (t1-t0) + "ms");
					callBack.callFinished(result);
				}
			});
		} catch (final XMLRPCFault e) {
			sendMessage(e.getFaultString(),  e.getFaultCode());
		} catch (final XMLRPCException e) {
			handler.post(new Runnable() {
				public void run() {

					Throwable couse = e.getCause();
					if (couse instanceof HttpHostConnectException) {
						sendMessage( e.getMessage(),XMLRPCMethod.STATUSCODE_NO_CONNECTION);
					} else {
						sendMessage( e.getMessage(), e.getCode());
					}
					
					
					Log.d("Test", "error", e);
				}
			});
		}
	}
}
